package com.powerbank.ordercenter.task;

import com.powerbank.ordercenter.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 订单定时任务
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class OrderScheduledTask {

    private final OrderService orderService;

    /**
     * 处理超时订单 - 每5分钟执行一次
     */
    @Scheduled(fixedRate = 300000) // 5分钟
    public void handleOverdueOrders() {
        try {
            log.info("开始处理超时订单");
            orderService.handleOverdueOrders();
            log.info("处理超时订单完成");
        } catch (Exception e) {
            log.error("处理超时订单失败", e);
        }
    }

    /**
     * 处理未支付过期订单 - 每10分钟执行一次
     */
    @Scheduled(fixedRate = 600000) // 10分钟
    public void handleUnpaidExpiredOrders() {
        try {
            log.info("开始处理未支付过期订单");
            orderService.handleUnpaidExpiredOrders();
            log.info("处理未支付过期订单完成");
        } catch (Exception e) {
            log.error("处理未支付过期订单失败", e);
        }
    }
}